#include <glib.h>

static void _queue_foreach_print_func(gpointer data, gpointer user_data)
{
    g_print("user_data:%s, data:%s \n", (gchar *)user_data, (gchar *)data);
}

gint main(gint argc, gchar **argv)
{
    GQueue *q = NULL;
    gboolean empty = FALSE;
    gchar *str[3] = {NULL, NULL, NULL};
    GList *list[3] = {NULL, NULL, NULL};
    guint i=0, len = 0;
    GList *l = NULL;

    str[0] = g_strdup("hello");
    str[1] = g_strdup("foo");
    str[2] = g_strdup("bar");

    q = g_queue_new();
    g_queue_push_tail(q, (gpointer)str[0]);
    g_queue_push_tail(q, (gpointer)str[1]);
    g_queue_push_tail(q, (gpointer)str[2]);

    g_queue_foreach(q, _queue_foreach_print_func, "before free");

    len = g_queue_get_length(q);
    for(i=0;i<len;i++) {
        list[i] = g_queue_peek_nth_link(q, i);
        g_print("peek %d: %s \n", i, (gchar *)list[i]->data);
    }

    for(i=0;i<len;i++) {
        g_queue_unlink(q, list[i]);
        if(NULL != list[i]->data) {
            g_print("free %s \n", (gchar *)list[i]->data);
            g_free((gchar *)list[i]->data);
        }
        g_list_free(list[i]);
    }
    g_list_free(l);

    empty = g_queue_is_empty(q);

    g_print( "empty:%s \n", empty ? "TRUE" : "FALSE" );
    
    g_queue_free(q);

    return 0;
}
